package com.leoman.spinningmachine.tab1;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

import com.leoman.helper.activity.WebActivity;
import com.leoman.helper.adapter.BaseRecyclerAdapter;
import com.leoman.helper.util.AppManager;
import com.leoman.helper.util.JsonKit;
import com.leoman.helper.util.RefreshUtil;
import com.leoman.helper.util.ToastUtil;
import com.leoman.spinningmachine.R;
import com.leoman.spinningmachine.base.BaseActivity;
import com.leoman.spinningmachine.bean.CameraOutBean;
import com.leoman.spinningmachine.bean.NoFunBean;
import com.leoman.spinningmachine.constant.Constant;
import com.leoman.spinningmachine.constant.Urls;
import com.leoman.spinningmachine.http.APIResponse;
import com.leoman.spinningmachine.http.MapData;
import com.leoman.spinningmachine.util.DialogUtils;
import com.leoman.spinningmachine.util.SharedPreferencesUtils;
import com.spurs.retrofit.APIClient;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import cn.bingoogolapple.refreshlayout.BGARefreshLayout;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.leoman.spinningmachine.constant.Urls.BASE_URL;

/**
 * 全部功能
 */
public class NoFunActivity extends BaseActivity {

    @BindView(R.id.rl_list)
    RecyclerView rlList;
    @BindView(R.id.pull_refresh_view)
    BGARefreshLayout pull_refresh_view;

    private NoFunAdapter noFunAdapter;
    private List<NoFunBean> list = new ArrayList<>();
    private int page = 1;
    private boolean isPageEnd;
    private int flag;

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.ac_nofun;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {

        initRefresh();
        AppManager.getInstance().addActivity(this);
        initTitle(getIntent().getExtras().getString("title"));

        flag = getIntent().getExtras().getInt("flag");

        setLayoutManager(rlList, LinearLayoutManager.VERTICAL, true);
        noFunAdapter = new NoFunAdapter(R.layout.item_nofun, list);
        rlList.setAdapter(noFunAdapter);

        noFunAdapter.setClickListener(new BaseRecyclerAdapter.onItemClickListener() {
            @Override
            public void onItemClick(int position, View v) {
                Intent intent = new Intent(NoFunActivity.this, WebActivity.class);
                intent.putExtra("title", list.get(position).cameraName);
                intent.putExtra("url", BASE_URL + "user/camera/" + list.get(position).cameraUuid);
                startActivity(intent);
            }
        });

    }

    @Override
    protected void initData() {
    }

    /**
     * 初始化下拉刷新
     */
    private void initRefresh() {
        pull_refresh_view = (BGARefreshLayout) findViewById(R.id.pull_refresh_view);
        pull_refresh_view.setDelegate(this);
        RefreshUtil.getInstance().initRefresh(this, pull_refresh_view);
        pull_refresh_view.beginRefreshing();
    }

    @Override
    public void onBGARefreshLayoutBeginRefreshing(BGARefreshLayout refreshLayout) {
        super.onBGARefreshLayoutBeginRefreshing(refreshLayout);
        page = 1;
        camera();
    }

    @Override
    public boolean onBGARefreshLayoutBeginLoadingMore(BGARefreshLayout refreshLayout) {
        if (isPageEnd) {
            return false;
        }
        page++;
        DialogUtils.getInstance().show(this);
        camera();
        return true;
    }

    public void camera() {
        HashMap<String, String> param = new HashMap<>();
        param.put("pageNo", String.valueOf(page));
        param.put("pageSize", String.valueOf(Constant.pageSize));
        param.put("token", SharedPreferencesUtils.getInstance().getString(Constant.TOKEN));
        APIClient.getInstance().getAPIService().PostAPI(Urls.CAMERA, param).enqueue(new Callback<Object>() {
            @Override
            public void onResponse(Call<Object> call, Response<Object> response) {
                DialogUtils.getInstance().cancel();
                String JSON_BACK = gson.toJson(response.body());
                MapData map = JsonKit.parse(JSON_BACK, MapData.class);
                if (map != null) {
                    CameraOutBean data = JsonKit.parse(map.data, CameraOutBean.class);
                    if (data != null && data.errorCode == 0) {
                        if (page == 1) {
                            list.clear();
                            if (pull_refresh_view != null)
                                pull_refresh_view.endRefreshing();
                        } else {
                            DialogUtils.getInstance().cancel();
                            if (pull_refresh_view != null)
                                pull_refresh_view.endLoadingMore();
                        }
                        if (data.data != null && data.data.list != null) {
                            list.addAll(data.data.list);
                            isPageEnd = data.data.list.size() < Constant.PAGE_SIZE;
                        } else {
                            isPageEnd = true;
                        }
                        if (list.size() < 0) {
                            ToastUtil.showToast(NoFunActivity.this, "暂无数据", Toast.LENGTH_SHORT);
                        }
                        noFunAdapter.notifyDataSetChanged();
                    }
                }
                call.cancel();
            }

            @Override
            public void onFailure(Call<Object> call, Throwable t) {
                DialogUtils.getInstance().cancel();
                call.cancel();
            }
        });
    }

    @Override
    public void OnSuccessData(APIResponse apiResponse, Integer tag) {
        if (tag == 1) {

        }
    }


}
